package kpi.java_training.testing.db;


import kpi.java_training.testing.questions.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Class loads questions from file
 */
public class FileLoader implements QuestionLoader {
    private BufferedReader dataReader;

    public FileLoader(String fileName) {
        try {
            FileInputStream fileStream = new FileInputStream(fileName);
            DataInputStream dataInput = new DataInputStream(fileStream);
            dataReader = new BufferedReader(new InputStreamReader(dataInput));
        } catch (Exception e) {
            dataReader = null;
        }
    }

    @Override
    public QuestionList getQuestions(String type) {
        try {
            int questionsNumber = readInt();
            QuestionList items = new QuestionList(type);

            for (int i = 0; i < questionsNumber; i++) {
                Question question = null;
                int questionType = readInt();
                switch (questionType) {
                    case Question.STRING:
                        question = CreateStringQuestion();
                        break;
                    case Question.INTEGER:
                        question = CreateIntQuestion();
                        break;
                    case Question.MANY_FROM_MANY:
                        question = CreateManyFromManyQuestion();
                        break;
                }
                items.add(question);
            }
            return items;
        } catch (Exception e) {
            //ConstantLoader loader = new ConstantLoader();
            //return loader.getQuestions();
            return null;
        }
    }

    @Override
    public int loadAnswers(QuestionList questions) {
        try {
            int answersNumber = readInt();

            for (int i = 0; i < answersNumber; i++) {
                questions.setQuestionAnswer(dataReader.readLine());
            }
            return answersNumber;
        } catch (Exception e) {
            return 0;
        }
    }

    private Question CreateStringQuestion() throws IOException {
        String message = dataReader.readLine();
        List<String> correctAnswers = readStringArray();
        int level = readInt();
        return new StringQuestion(message, correctAnswers, level);
    }

    private Question CreateIntQuestion() throws IOException {
        String message = dataReader.readLine();
        List<String> answerVariants = readStringArray();
        List<Integer> correctAnswers = readIntArray();
        int level = readInt();
        return new IntQuestion(message, answerVariants, correctAnswers, level);
    }

    private Question CreateManyFromManyQuestion() throws IOException {
        String message = dataReader.readLine();
        List<String> answerVariants = readStringArray();
        List<Integer> correctAnswers = readIntArray();
        int level = readInt();
        return new ManyFromManyQuestion(message, answerVariants, correctAnswers, level);
    }

    private List<String> readStringArray() throws IOException {
        int StringsNumber = readInt();
        ArrayList<String> strings = new ArrayList<String>();
        for (int i = 0; i < StringsNumber; i++) {
            strings.add(dataReader.readLine());
        }
        return strings;
    }

    private List<Integer> readIntArray() throws IOException {
        List<String> strings = readStringArray();
        ArrayList<Integer> integers = new ArrayList<Integer>();
        for (int i = 0; i < strings.size(); i++) {
            integers.add(Integer.parseInt(strings.get(i)));
        }
        return integers;
    }

    private int readInt() throws IOException {
        String str = dataReader.readLine();
        return Integer.parseInt(str);
    }

}
